package prototech.ahedra.model;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsuarioDao {
	private static Connection con = null;

	public static Usuario GetUsuarioUsr(String usr) throws SQLException {
		Usuario user = null;		
		ResultSet rs = null;
		
		try {
			con=DbCon.getConnection();
			if (!con.isClosed()) {
				// Insert value into SQL Table
				String qry = "SELECT * FROM cUsuarios WHERE usr=?";
				
				PreparedStatement ps = con.prepareStatement(qry);
				// Set string and position into table
				ps.setString(1, usr);

				// Execute all the above statement
				rs = ps.executeQuery();

				while (rs.next()) {
					user = new Usuario();
					user.setUsr(rs.getString("usr"));
					user.setPass(rs.getString("pass"));
				}				
			} else {
				throw new SQLException("Sin conexión a la base de datos");
			}
			return user;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// Close SQL server connection
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				throw new SQLException(e.getSQLState() + e.fillInStackTrace());
			}
		}
		return null;
	}
}
